var myGrid;
var zTreeObj;
var cNode;
var setting = {
	check : {
		enable : false
	},
	async : {
		enable : true,
		url : "newscatalog/news-catalog!listbyparent",
		autoParam : [ "id=bean.id" ],
		otherParam : {}
	},
	callback : {
		onClick : zTreeOnClick
	}
};
var root = {
	name : "所有新闻分类",
	open : true,
	nocheck : false,
	id : "root",
	isParent : true
};
function zTreeOnClick(event, treeId, treeNode) {
	if (!treeNode.isParent) {
		var pms =  {};
		pms["searchMap['catalog']"]=treeNode.id;
		myGrid.jqGrid('setGridParam',{url:"newsinfo/news-info!datalist",postData:pms}).trigger("reloadGrid");
	}
}
$(function() {
	$("#date").datepicker({
		"dateFormat" : "yy-mm-dd"
	});
	var zNodes = [ root ];
	zTreeObj = $.fn.zTree.init($("#resTree"), setting, zNodes);
	var nodes = zTreeObj.getNodes();
	zTreeObj.expandNode(nodes[0], null, false, false, true);
	$("#saveBtn").bind("click", function() {
		doGet("newsinfo/start");
	});

	myGrid = $("#list").jqGrid({
		url : "newsinfo/news-info!datalist",
		datatype : "json",
		mtype : 'GET',
		colNames : [ '名称', '日期', '管理' ],
		colModel : [ {
			name : 'name',
			index : 'name',
			width : 200,
			formatter : nameFormatter
		}, {
			name : 'date',
			index : 'date',
			width : 150
		}, {
			name : 'id',
			index : 'id',
			width : 50,
			formatter : mgrFormatter
		} ],
		pager : '#pager',
		rowNum : 10,
		rowList : [ 10, 20, 30 ],
		sortname : 'name',
		sortorder : 'asc',
		viewrecords : true,
		caption : "新闻列表",
		width : '550',
		height : '300',
		
		jsonReader : {
			repeatitems : false,
			id : "0"
		}
	});

	myGrid.jqGrid("navGrid", "#pager", {
		edit : false,
		add : false,
		del : false,
		search : false
	});
	myGrid.jqGrid("navButtonAdd", "#pager", {
		onClickButton : function() {
			doGet("newsinfo/start");
		},
		buttonicon : "ui-icon-newwin",
		caption : "添加",
		id : "newGroup"
	});
});
function nameFormatter(v, opt, row) {
	var link = $("<a href='newsinfo/news-info!show?bean.id=" + row['id']
			+ "' target='_blank'>" + v + "</a>");
	return $("<p></p>").append(link).html();
}
function catalogFormatter(v, opt, row) {
	return v.name ? v.name : (v ? v : '-');
}
function mgrFormatter(v, opt, row) {
	var link = $("<a href=\"javascript:removeOne('" + v
			+ "');\"><img style='border:none' src='" + sbase
			+ "images/removeme.gif'></a>");	
	return $("<p></p>").append(link).html();

}
function removeOne(id) {
	var url = "newsinfo/news-info!remove";
	var data = "bean.id=" + id;
	doSubmit(url, data, function() {
		myGrid.trigger("reloadGrid");
	});
}